package com.example.shop.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.example.shop.Constant;
import com.example.shop.R;
import com.example.shop.adapter.ItemLvActivityLimitbuyAdapter;
import com.example.shop.base.SCBaseActivity;
import com.example.shop.entity.LimitbuyEntity;
import com.example.shop.util.HttpNetUtil;
import com.example.sjlibrary.net.BaseJSONOverListener;
import com.example.sjlibrary.net.BaseNetForJSON;
import com.example.sjlibrary.net.BaseParams;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;

public class LimitBuyActivity extends SCBaseActivity implements View.OnClickListener {

    @Bind(R.id.lv_activity_limitbuy)
    ListView mLvActivityLimitbuy;
    private ItemLvActivityLimitbuyAdapter mItemLvActivityLimitbuyAdapter;
    long timeBegin;
    private Timer mTimer=new Timer();
    private List<LimitbuyEntity.ProductlistEntity> mProductlistEntities;
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==1){
//                long timeNow=System.currentTimeMillis();
//                int timePast= (int) (timeNow-timeBegin);
                if (mProductlistEntities!=null){
                    for (int i=0;i<mProductlistEntities.size();i++){
                       LimitbuyEntity.ProductlistEntity productlistEntity=mProductlistEntities.get(i);
                        int total=Integer.parseInt(productlistEntity.getLefttime());
                    productlistEntity.setLefttime(total-1000+"");
                        mItemLvActivityLimitbuyAdapter.setLimitTime(total-1000,i);
                    }
//                    mItemLvActivityLimitbuyAdapter.notifyDataSetChanged();

                }


            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_limit_buy);
        ButterKnife.bind(this);
        this.setTitleCenter("限时抢购");
        this.setTvTitleBarLeft("返回",this);
        BaseParams baseParams=new BaseParams();
        baseParams.addParam("page","1");
        baseParams.addParam("pageNum","5");
        baseParams.addParam("orderby","sale_down");
        this.setResult(121,null);
        BaseNetForJSON baseNetForJSON= HttpNetUtil.getNetRequest(Constant.LIMITBUY_URL,baseParams, LimitbuyEntity.class,new LimitBuyNetOver());
        baseNetForJSON.netForJSON();
    }

    @Override
    public void onClick(View v) {
        killSelf();
    }

    private class LimitBuyNetOver implements BaseJSONOverListener<LimitbuyEntity>{

        @Override
        public void onError() {
            Toast.makeText(mActivitySelf, "网络连接错误！", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSuccess(LimitbuyEntity entity) {
            mProductlistEntities=entity.getProductlist();
            if (mItemLvActivityLimitbuyAdapter==null){
                mItemLvActivityLimitbuyAdapter=new ItemLvActivityLimitbuyAdapter(mActivitySelf,entity.getProductlist());
                mLvActivityLimitbuy.setAdapter(mItemLvActivityLimitbuyAdapter);
                timeBegin=System.currentTimeMillis();
                mTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        mHandler.sendEmptyMessage(1);
                    }
                },0,1000);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTimer.cancel();
    }
}